package de.dlrg.wms.gui.views;

import static de.dlrg.wms.gui.views.coulmnNames.SwimmerColumnNames.SWIMMER_ID;
import static de.dlrg.wms.gui.views.coulmnNames.SwimmerColumnNames.SWIMMER_NAME;
import static de.dlrg.wms.gui.views.coulmnNames.SwimmerColumnNames.SWIMMER_SURNAME;
import static de.dlrg.wms.gui.views.coulmnNames.SwimmerColumnNames.SWIMMER_AGE_CLASS;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

import de.dlrg.wms.controller.controllers.SwimmerController;
import de.dlrg.wms.gui.FormContainer;
import de.dlrg.wms.gui.forms.SwimmerForm;
import de.dlrg.wms.gui.forms.generic.AbstractTableViewWithDeleteEdit;
import de.dlrg.wms.gui.forms.generic.GenericForm;
import de.dlrg.wms.gui.views.coulmnNames.SwimmerColumnNames;
import de.dlrg.wms.model.Swimmer;

public class SwimmerTableView extends AbstractTableViewWithDeleteEdit {
	private SwimmerController swimmerController;

	public void refresh() {
		this.table.clearAll();
		this.table.setItemCount(0);

		List<Swimmer> ageClasses = this.swimmerController.readAll();
		for (Swimmer swimmer : ageClasses) {
			TableItem swimmerItem = new TableItem(this.table, SWT.NULL);

			swimmerItem.setData(swimmer);
			swimmerItem.setText(SWIMMER_ID.ordinal(), "" + swimmer.getId());
			swimmerItem.setText(SWIMMER_NAME.ordinal(), "" + swimmer.getName());
			swimmerItem.setText(SWIMMER_SURNAME.ordinal(), "" + swimmer.getSurName());
			swimmerItem.setText(SWIMMER_AGE_CLASS.ordinal(), "" + swimmer.getAgeClass().getName());
		}
	}

	@SuppressWarnings("unchecked")
	public SwimmerTableView(Composite parent, int border,
			final SwimmerController swimmerController, final Shell shell) {
		super(parent, border);
		this.tableColumns = SwimmerColumnNames.class;
		this.swimmerController = swimmerController;
		this.initGUI(shell);
		
		this.refresh();
	}

	@Override
	protected void editPressed() {
		if (table.getSelection().length > 0
				&& table.getSelection().length < 2) {
			Swimmer selectedAgeClass = (Swimmer) table.getSelection()[0]
					.getData();
			FormContainer formContainer = new FormContainer();
			formContainer.createContents();
			GenericForm swimmerForm = new SwimmerForm(formContainer
					.getComposite(), SWT.BORDER, swimmerController,
					selectedAgeClass);
			swimmerForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
					true, true, 1, 1));
			swimmerForm.getClosing().addObserver(formContainer);
			formContainer.open();

			refresh();
		}
	}

	@Override
	protected void deletePressed() {
		if (table.getSelection().length > 0) {

			MessageBox messageBox = new MessageBox(shell,
					SWT.ICON_INFORMATION | SWT.OK | SWT.CANCEL);
			messageBox.setMessage("Do you want to delete "
					+ table.getSelectionCount() + " discipline(s)");
			int rc = messageBox.open();

			switch (rc) {
			case SWT.OK:
				break;
			default:
				return;
			}

			for (TableItem tableItem : table.getSelection()) {
				Swimmer toDelete = (Swimmer) tableItem.getData();
				swimmerController.delete(toDelete);
			}
			refresh();
		}
	}

//	@Override
//	protected String[] getColumnNames() {
//		String ret[] = new String[SwimmerColumnNames.values().length];
//		for (SwimmerColumnNames columnName : SwimmerColumnNames.values()) {
//			ret[columnName.ordinal()] = columnName.toString();
//		}
//		return ret;
//	}
}
